package com.example.podcastq;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.content.ServiceConnection;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

import com.android.vending.billing.IInAppBillingService;
import com.example.helloandroid.R;

public class Donaciones extends Activity {

	protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
		if (requestCode == 1001) {           
			int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
			String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
			String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");

			if (resultCode == RESULT_OK) {
				try {
					JSONObject jo = new JSONObject(purchaseData);
					String sku = jo.getString("productId");
					String token = jo.getString("purchaseToken");
					int response = mService.consumePurchase(3, getPackageName(), token);
					Toast.makeText(this,"You have bought the " + sku + ". Excellent choice, "+
							" adventurer!",Toast.LENGTH_SHORT);
				}
				catch (JSONException e) {
					Toast.makeText(this,"Failed to parse purchase data.",Toast.LENGTH_SHORT);
					e.printStackTrace();
				} catch (RemoteException e) {
					Toast.makeText(this,"Failed to parse purchase data.",Toast.LENGTH_SHORT);
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_donaciones);
		bindService(new 
				Intent("com.android.vending.billing.InAppBillingService.BIND"),
				mServiceConn, Context.BIND_AUTO_CREATE);


		Bundle buyIntentBundle = null;
		try {
			buyIntentBundle = mService.getBuyIntent(3, getPackageName(),
					"item1", "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");


			PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");	

			startIntentSenderForResult(pendingIntent.getIntentSender(),
					1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
					Integer.valueOf(0));
		} catch (SendIntentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mServiceConn != null) {
			unbindService(mServiceConn);
		}   
	}

	IInAppBillingService mService;

	ServiceConnection mServiceConn = new ServiceConnection() {
		@Override
		public void onServiceDisconnected(ComponentName name) {
			mService = null;
		}

		@Override
		public void onServiceConnected(ComponentName name, 
				IBinder service) {
			mService = IInAppBillingService.Stub.asInterface(service);
		}
	};

	private class AsyncTaskUpdateUrl extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {

			try {
				Bundle buyIntentBundle = mService.getBuyIntent(3, getPackageName(),
						"item10", "inapp", "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ");
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return null;

		}

	}

}
